package com.itmk.teacher.controller;

import com.alibaba.fastjson.JSONObject;
import com.itmk.student_point.entity.StudentPoint;
import com.itmk.teacher.entity.*;
import com.itmk.teacher.service.TeacherService;
import com.itmk.teacher.service.impl.TeacherServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

///teacher.do的请求，都会被该servlet处理
@WebServlet("/teacher.do")
public class TeacherServlet extends HttpServlet {
    private static final String PREX = "WEB-INF/jsp/admin";
    private static final String TEACHER_PREX = "WEB-INF/jsp/teacher";
    private TeacherService teacherService = new TeacherServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (method.equals("teacherUI")) {
            teacherUI(req, resp);
        } else if (method.equals("addUI")) {
            addUI(req, resp);
        } else if (method.equals("addSave")) {
            addSave(req, resp);
        } else if (method.equals("list")) {
            list(req, resp);
        } else if (method.equals("editUI")) {
            editUI(req, resp);
        } else if (method.equals("editSave")) {
            editSave(req, resp);
        } else if (method.equals("delete")) {
            delete(req, resp);
        } else if (method.equals("getCourse")) {
            getCourse(req, resp);
        } else if (method.equals("getStu")) {
            getStu(req, resp);
        } else if (method.equals("lookStuUI")) {
            lookStuUI(req, resp);
        } else if (method.equals("inputPointUI")) {
            inputPointUI(req, resp);
        }else if(method.equals("listInputStu")){
            listInputStu(req,resp);
        }else if(method.equals("addPoint")){
            addPoint(req,resp);
        }
    }

    private void addPoint(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取前端传递的参数
        String stuId = req.getParameter("stuId");
        String courseId = req.getParameter("courseId");
        BigDecimal pints = new BigDecimal(req.getParameter("points"));
        //封装参数
        StudentPoint studentPoint = new StudentPoint();
        studentPoint.setPoints(pints);
        studentPoint.setCourseId(courseId);
        studentPoint.setStuId(stuId);
        //调用service
        teacherService.addPoint(studentPoint);
        //封装返回值
        //组装返回数据
        JSONObject object = new JSONObject();
        object.put("code", 200);
        object.put("msg", "录入成功");
        //写回前端
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.print(object.toJSONString());
    }

    private void listInputStu(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取前端传递过来的参数
        String courseId =req.getParameter("courseId");
        String stuName = req.getParameter("stuName");
        //封装参数
        TeacherParm parm = new TeacherParm();
        parm.setCourseId(courseId);
        parm.setStuName(stuName);
        //设置教师id
        HttpSession session = req.getSession();
        Teacher user = (Teacher) session.getAttribute("user");
        parm.setTeacherId(user.getTeacherId());
        //调用service
        List<TeacherStudentVo> listStu = teacherService.listInputStu(parm);
        //获取总条数
        int count = teacherService.getStuCount(parm);
        //组装返回数据
        JSONObject object = new JSONObject();
        object.put("code", 0);
        object.put("msg", "查询成功");
        object.put("data", listStu);
        object.put("count", count);
        //写回前端
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.print(object.toJSONString());
    }

    private void inputPointUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String courseId = req.getParameter("courseId");
        req.setAttribute("courseId", courseId);
        req.getRequestDispatcher(TEACHER_PREX + "/input_point.jsp").forward(req, resp);
    }

    private void lookStuUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String courseId = req.getParameter("courseId");
        req.setAttribute("courseId", courseId);
        req.getRequestDispatcher(TEACHER_PREX + "/look_stu.jsp").forward(req, resp);
    }

    private void getStu(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取前端传递过来的参数
        int page = Integer.valueOf(req.getParameter("page"));
        int limit = Integer.valueOf(req.getParameter("limit"));
        String courseId = req.getParameter("courseId");
        String stuName = req.getParameter("stuName");
        //封装参数
        TeacherParm parm = new TeacherParm();
        parm.setCourseId(courseId);
        parm.setLimit(limit);
        parm.setPage(page);
        parm.setStuName(stuName);
        //设置教师id
        HttpSession session = req.getSession();
        Teacher user = (Teacher) session.getAttribute("user");
        parm.setTeacherId(user.getTeacherId());
        //调用service
        List<TeacherStudentVo> listStu = teacherService.listStu(parm);
        //获取总条数
        int count = teacherService.getStuCount(parm);
        //组装返回数据
        JSONObject object = new JSONObject();
        object.put("code", 0);
        object.put("msg", "查询成功");
        object.put("data", listStu);
        object.put("count", count);
        //写回前端
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.print(object.toJSONString());
    }

    private void getCourse(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取前端传递过来的参数
        int page = Integer.valueOf(req.getParameter("page"));
        int limit = Integer.valueOf(req.getParameter("limit"));
        String courseName = req.getParameter("courseName");
        //封装参数
        TeacherCouresParm parm = new TeacherCouresParm();
        parm.setLimit(limit);
        parm.setPage(page);
        parm.setCourseName(courseName);
        //设置教师id
        HttpSession session = req.getSession();
        Teacher user = (Teacher) session.getAttribute("user");
        parm.setTeacherId(user.getTeacherId());
        //调用service
        List<TeacherVo> teachetList = teacherService.getTeachetList(parm);
        //获取总条数
        int count = teacherService.getTeachetCount(parm);
        //组装返回数据
        JSONObject object = new JSONObject();
        object.put("code", 0);
        object.put("msg", "查询成功");
        object.put("data", teachetList);
        object.put("count", count);
        //写回前端
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.print(object.toJSONString());
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取id
        String teacherId = req.getParameter("teacherId");
        teacherService.delete(teacherId);
        //返回数据
        JSONObject object = new JSONObject();
        object.put("code", 200);
        object.put("msg", "删除成功");
        //写回前端
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.print(object.toJSONString());
    }

    private void editSave(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取前端传递过来的参数
        String teacherId = req.getParameter("teacherId");
        String teacherName = req.getParameter("teacherName");
        String sex = req.getParameter("sex");
        String phone = req.getParameter("phone");
        String teacherNum = req.getParameter("teacherNum");
        String password = req.getParameter("password");
        String teacherDesc = req.getParameter("teacherDesc");
        //封装参数
        Teacher teacher = new Teacher();
        teacher.setTeacherName(teacherName);
        teacher.setSex(sex);
        teacher.setPhone(phone);
        teacher.setTeacherNum(teacherNum);
        teacher.setPassword(password);
        teacher.setTeacherDesc(teacherDesc);
        teacher.setTeacherId(teacherId);
        //调用service
        teacherService.updateById(teacher);
        //封装返回值
        JSONObject object = new JSONObject();
        object.put("code", 200);
        object.put("msg", "编辑成功");
        //写回前端
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.print(object.toJSONString());
    }

    private void editUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查询回显的数据
        int teacherId = Integer.valueOf(req.getParameter("teacherId"));
        Teacher editTeacher = teacherService.getById(teacherId);
        req.setAttribute("editTeacher", editTeacher);
        req.getRequestDispatcher(PREX + "/teacher_edit.jsp").forward(req, resp);
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取前端传递的参数
        int page = Integer.valueOf(req.getParameter("page"));
        int limit = Integer.valueOf(req.getParameter("limit"));
        String teacherName = req.getParameter("teacherName");
        //封装参数
        TeacherParm parm = new TeacherParm();
        parm.setLimit(limit);
        parm.setPage(page);
        parm.setTeacherName(teacherName);
        //调用service
        List<Teacher> list = teacherService.list(parm);
        //获取总条数
        int count = teacherService.getCount(parm);
        //组装返回数据
        JSONObject object = new JSONObject();
        object.put("code", 0);
        object.put("msg", "查询成功");
        object.put("data", list);
        object.put("count", count);
        //写回前端
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.print(object.toJSONString());
    }

    private void addSave(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取前端传递过来的参数
        String teacherName = req.getParameter("teacherName");
        String sex = req.getParameter("sex");
        String phone = req.getParameter("phone");
        String teacherNum = req.getParameter("teacherNum");
        String password = req.getParameter("password");
        String teacherDesc = req.getParameter("teacherDesc");
        //封装参数
        Teacher teacher = new Teacher();
        teacher.setTeacherId(UUID.randomUUID().toString().replaceAll("-",""));
        teacher.setTeacherName(teacherName);
        teacher.setSex(sex);
        teacher.setPhone(phone);
        teacher.setTeacherNum(teacherNum);
        teacher.setPassword(password);
        teacher.setTeacherDesc(teacherDesc);
        //调用service
        teacherService.add(teacher);
        //封装返回值
        JSONObject object = new JSONObject();
        object.put("code", 200);
        object.put("msg", "新增成功");
        //写回前端
        resp.setContentType("application/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.print(object.toJSONString());
    }

    private void addUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher(PREX + "/teacher_add.jsp").forward(req, resp);
    }

    /**
     * 返回教师管理页面
     *
     * @param req
     * @param resp
     */
    private void teacherUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher(PREX + "/teacher.jsp").forward(req, resp);
    }
}
